package db.tables;
import   java.lang.Math.*;

import java.util.ArrayList;

/**
 * This class makes all calculation needed to display on one of the report's
 * panel
 * 
 */


public class Statistics {
	
	/**
	 * @param m
	 *            string of Calculate the avrage of integer array list.
	 * @return average
	 */
	public  double average(ArrayList<Integer> m) {
		long sum = 0;
		for (int i = 0; i < m.size(); i++) {
			sum += m.get(i);
		}
		return (double) sum / m.size();
	}
		
	
	/**
	 * @param average 
	 *            string of Calculate the average of integer array
	 * @return average
	 */
	public static float average(int [] m) {
		long sum = 0;
		for (int i = 0; i < m.length; i++) {
			sum += m[i];
		}
		return (float) sum / m.length;
	}

	
	
	/**
	 * @param int array
	 *            string of Calculate the standardDeviation of integer array
	 *            list.
	 * @return standardDeviation
	 */
	public  double standardDeviation(ArrayList<Integer> m) {
		double avg = average(m);
		double[] arr = new double[m.size()];
		double sum = 0;

		for (int i = 0; i < m.size(); i++) {
			arr[i] = Math.pow((m.get(i) - avg), 2);
			sum += arr[i];
		}
		sum /= (m.size());
		sum = Math.sqrt(sum);
		return sum;
	}
	
	
	/**
	 * @param int array
	 *            string of Calculate the standardDeviation of integer array/
	 * @return standardDeviation
	 */
	public static float standardDeviation(int[] m){
		float avg =average(m);
		float [] arr=new float[m.length];
		float sum=0;
		
		for(int i = 0; i < m.length; i++){
			arr[i]=(float) Math.pow((m[i]-avg),2);
			sum+=arr[i];
		}
		sum/=(m.length);
		sum=(float) Math.sqrt(sum);
		return sum;
}

	/**
	 * @param m
	 *            string of Calculate the median of integer array list.
	 * @return median
	 */
	public double median(ArrayList<Integer> m) {
		int middle = m.size() / 2;
		if (m.size() % 2 == 1) {
			return m.get(middle);
		} else {
			return (double) ((m.get(middle - 1) + m.get(middle)) / 2.0);
		}
	}
	
	
	/**
	 * @param m
	 *            string of Calculate the median of integer array list.
	 * @return median
	 */
	public static float median(int [] m) {
		int middle = m.length / 2;
		if (m.length % 2 == 1) {
			return m[middle];
		} else {
			return (float) ((m[middle - 1] + m[middle]) / 2.0);
		}
	}
	
	
	
	/**
	 * @param Array
	 *            list of num of orders per day int arr[] 0-9 of Calculate the
	 *            frequency of the array
	 * 
	 * @return median
	 */
	public  int[] frequency(ArrayList<Integer> m) {
		int retArr[] = new int[10];
		for (int i = 0; i < m.size(); i++) {
			retArr[m.get(i) / 10]++;
		}
		return retArr;
	}
	
	/**
	 * @param Int Array of num of orders per day
	 *            int arr[] 0-9 of Calculate the frequency of the array
	 *            
	 * @return median
	 */
	public static int [] frequency(int[] m) {
		int retArr[]=new int[10];
		for(int i=0;i<m.length;i++){
			retArr[m[i]/10]++;
		}
		return retArr;
	}
}
	
